<?php
/**
 * Description: 验证用户是否存在
 * User: devforma
 * Date: 15/6/11
 * Time: 15:37
 */

class IsUserExists implements iController {

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        global $uddb;

        if (empty($queryArgs['user_name']))
            return genOutput(1000);

        //检测邮箱
        if ((empty($queryArgs['type']) || $queryArgs['type'] === 'email') && validateEmail(trim($queryArgs['user_name']))) {
            $sql = 'SELECT `ID`, `weibo`, `weixin` FROM `user_auth_info` WHERE `user_email`=:username LIMIT 1';

        //检测手机号
        } elseif ((empty($queryArgs['type']) || $queryArgs['type'] === 'phone') && validatePhone(trim($queryArgs['user_name']))) {
            $sql = 'SELECT `ID`, `weibo`, `weixin` FROM `user_auth_info` WHERE `phone`=:username LIMIT 1';

        //检测微博
        } elseif (!empty($queryArgs['type']) && $queryArgs['type'] === 'weibo') {
            $sql = 'SELECT `ID`, `weibo`, `weixin` FROM `user_auth_info` WHERE `weibo`=:username LIMIT 1';

        //检测微信
        } elseif (!empty($queryArgs['type']) && $queryArgs['type'] === 'weixin') {
            $sql = 'SELECT `ID`, `weibo`, `weixin` FROM `user_auth_info` WHERE `weixin`=:username LIMIT 1';

        } else {
            return genOutput(1001);
        }

        $exists = $uddb->getRow($sql, array(':username' => trim($queryArgs['user_name'])));

        return genOutput(1, array(
            'user_exists' => !empty($exists),
            'weibo' => !empty($exists) && !empty($exists->weibo) ? $exists->weibo : '',
            'weixin' => !empty($exists) && !empty($exists->weixin) ? $exists->weixin : ''
        ));
    }
}